package com.mobile.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import nl.captcha.Captcha;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.model.Whether;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.service.SysUserManager;
import com.hsnn.medstgmini.util.Pagination;
import com.yspa.yspa.service.YspaInterfaceManager;

/**
 * @category 登陆|登出
 * @author 单大伟
 * @date 2015-06-10
 */

@Controller
public class MobileLoginController {

	private static final Logger log = Logger.getLogger(MobileLoginController.class);
	

	@Autowired
	private SysUserManager userManager;

	@Autowired
	private HttpSession session;
	
    @Value("#{sysProp['captcha.enable']}")
    private String captchaEnable;
    
    @Value("#{sysProp['system.title']}")
    private String systemTitle;
    
    @Value("#{sysProp['system.host.unit']}")
    private String systemHostUnit;
    
	@Autowired
	private YspaInterfaceManager manager;
	
	/**
	 * @category 跳转登陆页面
	 * @date 2015年6月12日
	 * @return
	 */
	@RequestMapping("/mobile/login")
	public String login(HttpServletRequest request) {
		Map<String,Object> userMap = (Map<String, Object>) session.getAttribute(Constants.USERINFO);
		if (userMap != null) {
			return "redirect:index.html";
		}
		request.setAttribute("systemHostUnit", systemHostUnit);
		request.setAttribute("systemTitle", systemTitle);
		return "/mobile/login";
	}

	/**
	 * @category 用户登录处理
	 * @author 单大伟
	 * @date 2015-06-10
	 * @param request
	 * @param model
	 * @return String 登录验证成功后跳转到的页面
	 */
	@RequestMapping("/mobile/loginAuth")
	public String loginAuth(HttpServletRequest request, Model model) {

		// user pass in parameters
		String username = request.getParameter(Constants.USERNAME);
		String password = request.getParameter(Constants.PASSWORD);
		
//		String answer = request.getParameter(Constants.ANSWER);

		log.info("User [" + username + "] try login!");

		if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
			model.addAttribute("errorMsg", "请输入用户名密码！");
			return "/login";
		}

		if (log.isDebugEnabled()) {
		    log.debug("capt enable = " + captchaEnable);
		}
		
		Map<String,String> map = new HashMap<String,String>();
//		map.put("userno", request.getParameter("userno"));
		map.put("userid", username);
//		map.put("name", request.getParameter("name"));
//		map.put("passwd", request.getParameter("passwd"));
		Map<String,Object> userMap = manager.getUserInfo2(map);
        
		// user pass in empty username and password
		if (userMap == null) {
			log.warn("empty user");
			model.addAttribute("errorMsg", "用户名不存在！");
			return "/mobile/login";
		}
		
		if (password.equals(userMap.get("passwd"))) {
			log.info("authentication passed");

			HttpSession session = request.getSession(true);
			session.setAttribute(Constants.USERINFO, userMap);
    		
			return "redirect:/mobile/index.html";
		} else {
			log.warn("authentication failed");
			model.addAttribute("errorMsg", "用户名或密码不正确！");
			return "/mobile/login";
		}
	}
	
	@RequestMapping("/mobile/loginByUserNo")
	@ResponseBody
	public Map<String, Object> getUserInfo(Long userno){
		Map<String,Object> userMap = manager.getUserInfo(userno);
		session.setAttribute(Constants.USERINFO, userMap);
		return userMap;
	}

	/**
	 * @category 用户退出登录处理
	 * @author 单大伟
	 * @date 2015-06-10
	 * @param request
	 * @param model
	 * @return String 退出登录后跳转到的页面
	 */
	@RequestMapping("/mobile/logout")
	public String logout(HttpServletRequest request, Model model) {
		HttpSession session = request.getSession();
		if (session != null) {
			session.setAttribute(Constants.USERINFO, null);
			session.invalidate();
		}
		return "redirect:index.html";
	}
}
